package test_achieve.admin;
import com.alibaba.fastjson.JSON;
import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
import constant.CityId;
import constant.Constant;
import constant.Pm;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import lombok.Data;
import util.ConfigSql;
import util.HttpClient;
import util.JdbcUtil;
import util.tools.BusinessTools;
import util.tools.CommonTools;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by chenkuo on 2018/1/16.
 */
public class HttpAdmin {
    private String holidayName ;
    private String couponTemplateName;
    @Given("^p优惠券分配：(.+)：(.+)：(.+)：(.+)$")
    public void p优惠券分配数量(String user,String type,String num,String city) throws Throwable {
        String cookie = HttpClient.getAdminCookie();
        Map<String, String> map = new HashMap<String, String>();
        String url =Constant.couponAssign;
        map.put("couponType",type);
        map.put("mobile",user);
        map.put("cityId", String.valueOf(CityId.getCityCodeByName(city)));
        map.put("channel","1");
        map.put("count",num);
        Map<String, String> Cookie = new HashMap<String, String>();
        Cookie.put("Cookie",cookie);
        HttpClient.post(url,map,Cookie);
    }
/**
 * 日期限制：（不可冲突）
 * ALL_DAY(0, "全部日期"),
 WEEKEND(2, "周末"),
 WORKDAY(3, "工作日"),
 NATIONAL_HOLIDAY(4, "法定节假日"),
 SPECIAL_HOLIDAY(5, "特殊节假日");
使用平台限制：
 WEB(1,"WEB", "PC端"),
 WEIXIN(2,"WEIXIN", "微信"),
 APP(3,"APP", "APP端"),
 WAP(4,"WAP", "WAP页"),
 BATCH(5, "BATCH", "批量下单"),
 ALIPAY(6,"ALIPAY","支付宝"),
 WEIXINSMALLAPPLICATION(7,"WEIXINSMALLAPPLICATION","微信小程序"),
 CMB(8, "CMB", "招行"),
 OPEN(10,"OPEN", "开放平台"),
 BAIDU(11,"BAIDU", "百度");
 * */
    @Given("^p新增直减优惠券组合"+Pm.p8)
    public void p新增直减优惠券组合(int validityDays,int amount,int count,String start,String end,String channel,String availableDate,String unAvailableDate) throws Throwable {
        int[] channelint =CommonTools.stringToIntArray(channel,",");
        int[] availableDateint =CommonTools.stringToIntArray(availableDate,",");
        int[] unAvailableDateint =CommonTools.stringToIntArray(unAvailableDate,",");

        Map<String, Object> map = couponConfig(channelint,availableDateint,unAvailableDateint,start,end);//公共的部分
        CouponTemplateConfig coup = new CouponTemplateConfig(1,validityDays,amount,count,12,"优惠券  直减  3元  有效期17天  2张","直减");
        httpCreateCoupon(map,coup);
    }

    @Given("^p新增直减优惠券组合"+Pm.p4)
    public void p新增直减优惠券组合(int amount,String start,String end,String channel) throws Throwable {
        p新增直减优惠券组合(18,amount,1,start,end,channel,"2,3,4","5");
    }
    @Given("^p新增直减优惠券组合"+Pm.p2)
    public void p新增直减优惠券组合(int amount,String channel) throws Throwable {
        p新增直减优惠券组合(18,amount,1,"00:01","23:58",channel,"2,3,4","5");
        System.out.println("aaaaaaaaaaaafaq");
    }

    @Given("^p新增满减优惠券组合"+Pm.p9)
    public void p新增满减优惠券组合(int validityDays,String conditionAmount,int amount,int count,String start,String end,String channel,String availableDate,String unAvailableDate) throws Throwable {
        int[] channelint =CommonTools.stringToIntArray(channel,",");
        int[] availableDateint =CommonTools.stringToIntArray(availableDate,",");
        int[] unAvailableDateint =CommonTools.stringToIntArray(unAvailableDate,",");
        int aa = Integer.valueOf(conditionAmount)/100;
        Map<String, Object> map = couponConfig(channelint,availableDateint,unAvailableDateint,start,end);//公共的部分
        CouponManJianConfig coupon = new CouponManJianConfig(1,validityDays,amount,count,11,"优惠券  满55减33元  有效期17天  2张","满"+aa+"减",conditionAmount);
        httpCreateCoupon(map,coupon);
    }

    @Given("^p新增满减优惠券组合"+Pm.p5)
    public void p新增满减优惠券组合(String conditionAmount,int amount,String start,String end,String channel) throws Throwable {
        p新增满减优惠券组合(18,conditionAmount,amount,1,start,end,channel,"2,3,4","5");
    }

    @Given("^p新增满减优惠券组合"+Pm.p3)
    public void p新增满减优惠券组合(String conditionAmount,int amount,String channel) throws Throwable {
        p新增满减优惠券组合(18,conditionAmount,amount,1,"00:01","23:58",channel,"2,3,4","5");
    }

    @Given("^p新增一口价优惠券组合"+ Pm.p10)
    public void p新增一口价优惠券组合(int validityDays,int amount,int count,int maxCouponDistance,int maxCouponWeight,String start,String end,String channel,String availableDate,String unAvailableDate) throws Throwable {
        int[] channelint =CommonTools.stringToIntArray(channel,",");
        int[] availableDateint =CommonTools.stringToIntArray(availableDate,",");
        int[] unAvailableDateint =CommonTools.stringToIntArray(unAvailableDate,",");
        Map<String, Object> map = couponConfig(channelint,availableDateint,unAvailableDateint,start,end);//公共的部分
        CouponYikoujiaConfig couponManJian = new CouponYikoujiaConfig(1,validityDays,amount,count,13,"优惠券  一口价  32公里12公斤内50元  有效期14天  1张","一口价(32km,12kg)",maxCouponDistance,maxCouponWeight);
        httpCreateCoupon(map,couponManJian);
    }

    @Given("^p新增一口价优惠券组合"+Pm.p6)
    public void p新增一口价优惠券组合(int amount,int maxCouponDistance,int maxCouponWeight,String availableDateint,String unAvailableDateint,String channel) throws Throwable {
        p新增一口价优惠券组合(18,amount,1,maxCouponDistance,maxCouponWeight,"00:05","23:50",channel,availableDateint,unAvailableDateint);
    }

    @Given("^p新增一口价优惠券组合"+Pm.p4)
    public void p新增一口价优惠券组合(int amount,int maxCouponDistance,int maxCouponWeight,String channel) throws Throwable {
        p新增一口价优惠券组合(18,amount,1,maxCouponDistance,maxCouponWeight,"00:01","23:58",channel,"2,3,4","5");
    }


    @Given("^p新增折扣卡优惠券组合"+ Pm.p9)
    public void p新增折扣卡优惠券组合(int validityDays,int maxDiscountAmount,int amount,int count,String start,String end,String channel,String availableDate,String unAvailableDate) throws Throwable {
        int[] channelint =CommonTools.stringToIntArray(channel,",");
        int[] availableDateint =CommonTools.stringToIntArray(availableDate,",");
        int[] unAvailableDateint =CommonTools.stringToIntArray(unAvailableDate,",");
        Map<String, Object> map = couponConfig(channelint,availableDateint,unAvailableDateint,start,end);//公共的部分
        CouponZhekoukaConfig couponManJian = new CouponZhekoukaConfig(2,validityDays,amount,count,4,"优惠券  一口价  32公里12公斤内50元  有效期14天  1张","一口价(32km,12kg)",maxDiscountAmount);
        httpCreateCoupon(map,couponManJian);
    }
    @Given("^p新增折扣卡优惠券组合"+Pm.p5)
    public void p新增折扣卡优惠券组合(int maxDiscountAmount,int amount,String start,String end,String channel) throws Throwable {
        p新增折扣卡优惠券组合(18,maxDiscountAmount,amount,1,start,end,channel,"2,3,4","5");
    }

    @Given("^p新增折扣卡优惠券组合"+Pm.p3)
    public void p新增折扣卡优惠券组合(int maxDiscountAmount,int amount,String channel) throws Throwable {
        p新增折扣卡优惠券组合(18,maxDiscountAmount,amount,1,"00:01","23:58",channel,"2,3,4","5");
    }


/**http方式在admin生成优惠券*/
    private void httpCreateCoupon(Map<String,Object> map,CouponTemplateConfig coup) throws Exception {
        String url = Constant.couponConfig;
        List<CouponTemplateConfig> list = Lists.newArrayList(coup);
        map.put("couponTemplateConfigDtos",list);
        for (int i = 0; i < list.size(); i++) {
            System.out.println("list"+list.get(i));
        }
        System.out.println("list+"+list.size());
        String value = JSON.toJSONString(map);
        System.out.println(value);
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("couponTemplateDtoStr",value);
        Map<String, String> Cookie = new HashMap<String, String>();
        String cookie = HttpClient.getAdminCookie();
        Cookie.put("Cookie",cookie);
        System.out.println("Cookie-----"+cookie);
        HttpClient.post(url,map1,Cookie);

    }

    @Given("^p设置今天为特殊节日$")
    public void p设置今天为特殊节日() throws Throwable {
        String url ="http://admin.sf.bingex.com/admin/holiday/saveHoliday";
        holidayName = CommonTools.getDateUnixTime(0);
        String cookie = HttpClient.getAdminCookie();
        Map<String, String> Cookie = new HashMap<String, String>();
        Cookie.put("Cookie",cookie);
        Map<String, String> map = new HashMap<String, String>();
        map.put("endTime",CommonTools.getDateUnixTime(1));
        map.put("name",holidayName);
        map.put("startTime",CommonTools.getDateUnixTime(-1));
        map.put("type","5");
        HttpClient.get(url,map,Cookie);
    }

    @Given("^p清除设置的特殊节日$")
    public void p清除设置的特殊节日() throws Throwable {
        String id = JdbcUtil.sqlString(ConfigSql.get("queryholidayId",holidayName));
        String url = Constant.deleteHoliday;
        String cookie = HttpClient.getAdminCookie();
        Map<String, String> Cookie = new HashMap<String, String>();
        Cookie.put("Cookie",cookie);
        Map<String,String> map = new HashMap<String, String>();
        map.put("id",id);
        HttpClient.get(url,map,Cookie);
    }

    @Given("^p给用户发送刚创建的组合券：(.*)$")
    public void p给用户发送刚创建的组合券(String mobile) throws Throwable {
        String url = Constant.assignCoupon;
        System.out.println("url:"+url);
        String cookie = HttpClient.getAdminCookie();
        Map<String, String> Cookie = new HashMap<String, String>();
        Cookie.put("Cookie",cookie);
        System.out.println(cookie);
        Map<String,String> map = new HashMap<String, String>();
        map.put("mobiles",mobile);
//        System.out.println(mobile);
        map.put("couponTemplateName",couponTemplateName);
        System.out.println(couponTemplateName);
        HttpClient.get(url,map,Cookie);
    }

    @Given("^p删除用户身上未使用的优惠券：(.*)$")
    public void p删除用户身上未使用的优惠券(String mobile) throws Throwable {
        String url = Constant.deleteUnuseCoupon;
        System.out.println(url);
        String cookie = HttpClient.getAdminCookie();
        Map<String, String> Cookie = new HashMap<String, String>();
        Cookie.put("Cookie",cookie);
        Map<String,String> map = new HashMap<String, String>();
        map.put("mobile", BusinessTools.getMobileSecret(mobile));
        HttpClient.get(url,map,Cookie);
    }


    /**基本类型与直减*/

    public class CouponTemplateConfig{
        private int couponType;//优惠券类型
        private int validityDays;//有效期
        private int count;//张数
        private int couponSubtype;//
        private int amount;//金额
        private String desc;//备注说明
        private String couponDesc;//类型备注

        public CouponTemplateConfig(int couponType,int validityDays, int amount, int count, int couponSubtype, String desc,String couponDesc){
//            this.couponType = couponType == 0 ? 12:couponType;
            this.couponType = couponType;
            this.validityDays = validityDays;
            this.count = count;
            this.amount = amount;
            this.couponSubtype = couponSubtype;
            this.desc = desc;
            this.couponDesc = couponDesc;
        }
      public int getCouponType(){
            return this.couponType;
      }
        public int getValidityDays(){
            return this.validityDays;
        }
        public int getAmount(){
            return this.amount;
        }
        public int getCount(){
            return this.count;
        }
        public int getCouponSubtype(){
            return this.couponSubtype;
        }
        public String getDesc(){
            return this.desc;
        }
        public String getCouponDesc(){
            return this.couponDesc;
        }
    }
    /**满减*/
    class CouponManJianConfig extends CouponTemplateConfig {
        private String conditionAmount;
        public CouponManJianConfig(int couponType, int validityDays, int amount, int count, int couponSubtype, String desc, String couponDesc,String conditionAmount) {
            super(couponType, validityDays, amount, count, couponSubtype, desc, couponDesc);
            this.conditionAmount = conditionAmount;
        }

        public String getConditionAmount() {
            return conditionAmount;
        }

        public void setConditionAmount(String conditionAmount) {
            this.conditionAmount = conditionAmount;
        }
    }

    /**一口价*/
    @Data
    class CouponYikoujiaConfig extends CouponTemplateConfig{
        private int maxCouponDistance;
        private int maxCouponWeight;
        public CouponYikoujiaConfig(int couponType, int validityDays, int amount, int count, int couponSubtype, String desc, String couponDesc,int maxCouponDistance,int maxCouponWeight) {
            super(couponType, validityDays, amount, count, couponSubtype, desc, couponDesc);
            this.maxCouponDistance = maxCouponDistance;
            this.maxCouponWeight = maxCouponWeight;
        }
}
    /**折扣卡*/
    @Data
    class CouponZhekoukaConfig extends CouponTemplateConfig {
        private int maxDiscountAmount;
        public CouponZhekoukaConfig(int couponType, int validityDays, int amount, int count, int couponSubtype, java.lang.String desc, java.lang.String couponDesc,int maxDiscountAmount) {
            super(couponType, validityDays, amount, count, couponSubtype, desc, couponDesc);
            this.maxDiscountAmount = maxDiscountAmount;
        }
    }
    /**优惠券可用起始时间与结束时间*/
    class availableTime{
        private String start;//可用起始时间
        private String end;//可用结束时间
        public availableTime(String start,String end){
            this.end = end;
            this.start = start;
        }

        public String getStart() {
            return start;
        }

        public String getEnd() {
            return end;
        }

        public void setStart(String start) {
            this.start = start;
        }

        public void setEnd(String end) {
            this.end = end;
        }
    }

    /**优惠券其它基本属性*/
    private Map<String, Object> couponConfig(int[] channel,int[] availableDate,int[] unAvailableDate,String start,String end){
        couponTemplateName ="自动化测试"+ CommonTools.getCurrentDateTime();//配置名称
        channel = channel == null ? new int[]{2,3} : channel;//可用平台 例子：1,4
        availableDate = availableDate == null ? new int[]{1,3} : availableDate;//可使用日期 例子：1,4
        unAvailableDate = unAvailableDate == null ? new int[]{2} : unAvailableDate;//不可使用日期 例子：1,4
        start = start == null ? "00:05" : start;//可用起始时间
        end = end == null ? "00:15" : end;//可用结束时间
        Map<String, Object> map = Maps.newHashMap();
        map.put("name",couponTemplateName);
        map.put("availableOrderChannel",channel);
        map.put("availableCity",new int[]{1101,1301});
        map.put("availableDate",availableDate);
        map.put("unAvailableDate",unAvailableDate);
        map.put("availableGoodsType",new String[]{"蛋糕","鲜花"});
        map.put("minDistanceCondition",5000);
        map.put("maxDistanceCondition",30000);
        availableTime bean =new availableTime(start,end);
        List <availableTime> times = Lists.newArrayList(bean);
        map.put("availableTime",times);
        return map;
    }

    @Given("^p订单因其他客服流单：(.*)$")
    public void p订单因其他客服流单(String orderNumber) throws Throwable {
        String cookie = HttpClient.getCsCookie();
        String ur2 = Constant.httpCsCancelOrderUrl;
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("v", "1502264605157");
        map1.put("abortType", "99");
        map1.put("reason", "2222");
        map1.put("orderNumber",orderNumber);
        Map<String, String> Cookie = new HashMap<String, String>();
        Cookie.put("Cookie", cookie);
        HttpClient httpClient = new HttpClient();
        httpClient.post(ur2, map1, Cookie);
    }

}
